<template>
	<view class="zg-area">
		<!-- 导航 -->
		<view class="area-nav" :style="{'padding-top': statusbar_height + 'px'}">
			<view class="nav">
				<view class="nav-left" @click="navBack">
					<image src="/pagesShop/static/left.svg" mode=""></image>
				</view>
				<view class="nav-center">
					<input type="text" placeholder="店铺名称" v-model="keyword">
					<image src="/pagesShop/static/search2.png" mode=""></image>
				</view>
				<view class="nav-right"></view>
			</view>
		</view>
		<view class="fixed-tool-top" :style="{'top': statusbar_height + 44 + 'px'}">
			<!-- 筛选 -->
			<view class="sx">
				<scroll-view scroll-x style="height: 40px">
					<view class="sx-box">
						<view class="sx-box-item" :class="{active: currentIndex3 === 0}" @click="changeTab3(0)">
							<text>综合</text>
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 1}" @click="changeTab3(1)">
							<text>销量</text>
							<!-- <view class="sx-box-icon">
								<template v-if="currentIndex3 === 1">
									<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view> -->
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 2}" @click="changeTab3(2)">
							<text>评分</text>
							<!-- <view class="sx-box-icon">
								<template v-if="currentIndex3 === 2">
									<template v-if="sortx === 1">
										<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
									</template>
									<template v-else>
										<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
									</template>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view> -->
						</view>
						<!-- <view class="sx-box-item" @click="changeTab4(row_type)">
							<image v-if="row_type === 'row'" src="/pagesShop/static/f1.png" mode="widthFix"></image>
							<image v-else src="/pagesShop/static/f2.png" mode="widthFix"></image>
						</view> -->
						<view class="sx-box-item" @click="changeTab5">
							<text>筛选</text>
							<view class="sx-box-icon">
								<image class="sbi1" src="/pagesShop/static/ff.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="content" :style="{'padding-top': statusbar_height + 84 + 'px'}">
			<view class="area-1" v-if="list.length > 0">
				<view class="tjshop">
					<view class="tjshop-content">
						<view class="tc" v-for="(item,index) in list" :key="index">
							<view class="tc-top">
								<view class="tc-top-left">
									<image class="tp-h88 tp-w88 tp-br8 tp-mr20" :src="`${shopImgUrl}${item.scCover}`" mode="aspectFill"></image>
									<view class="ttl-info">
										<view class="ttl-info-title">
											<text>{{item.scName}}</text>
										</view>
										<view class="ttl-info-content">
											<view>销量：{{ handleSales(item.scSales) }}</view>
											<view>评分：<text>{{item.scScore || 0}}分</text></view>
										</view>
									</view>
								</view>
								<view class="tc-top-right">
									<button plain @click="goRouter1(item)">进入店铺</button>
								</view>
							</view>
							<view class="tc-bottom">
								<view class="tc-bottom-img" :style="{marginRight: (index1 + 1) % 3 == 0 ? '0' : '10.5rpx'}" @click="goRouter10(item1)" v-for="(item1,index1) in item.shopProductVos" :key="index1">
									<image class="tp-br8" :src="`${shopImgUrl}${item1.pic}`" mode="aspectFill"></image>
									<view class="tcbp">￥{{item1.discountPrice}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<uni-load-more color="#999" :status="status" v-if="list.length > 0"></uni-load-more>
			</view>
			<view class="nomore" v-if="list.length === 0 && !loading">
				<view class="nomore-text">
					<image src="/pagesShop/static/zanwu.png" mode="widthFix"></image>
					<view>暂无数据</view>
				</view>
			</view>
		</view>
		<!-- 筛选 -->
		<view class="filter-wapper" v-if="show1">
			<view class="filter-extra" @click="show1=false"></view>
			<view class="filter-content">
				<view class="filter-box">
					<view class="filter-h4">分类</view>
					<view class="filter-con">
						<view class="filter-tags">
							<view class="filter-tag" :class="{'active': currentIndex1 === index}" v-for="(item,index) in nav1" :key="index" 
							@click="changeTab1(item,index)">
								{{item.name}}
							</view>
						</view>
					</view>
					<view class="filter-h4">区域</view>
					<view class="filter-con">
						<view class="filter-selects">
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[0] || '请选择省'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[1] || '请选择市'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[2] || '请选择区'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="filter-btns">
					<button plain @click="resetFilter">重置</button>
					<button plain @click="comfirmFilter">确认</button>
				</view>
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"
		 @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'; // 三级城市选择联动
	import cityData from '@/common/city.data.js';
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				show1: false,
				row_type: 'table',
				currentIndex1: null,
				currentIndex3: 0,
				keyword: '',
				type: '0',
				page: 1,
				status: 'more',
				loading: false,
				list: [],
				nav1: [], // 一级分类列表
				price1: '', // 最低价
				price2: '', // 最高价
				good: '', // 好评
				address: [],
				// 筛选
				filters: {
					firstCategoryId: '', // 二级分类id
					address: [],
				},
				// 三级联动
				pickerText: '', // 三级联动得到的值
				pickerViewText: {}, // 页面需要的值 
				province: '', // 省
				city: '', // 市
				area: '', // 区
				mulLinkageTwoPicker: cityData, // 导入的数据
				cityPickerValueDefault: [0, 0, 1], // 三级联动选取的值
				themeColor: '#007AFF', // 确定的主题色
				firstCategoryId: '',
				address: [],
				sortx: 1
			}
		},
		watch: {
			keyword(v) {
				this.getList1()
			}
		},
		onReady() {
			this.getList2()
		},
		onLoad(params) {
			console.log(params);
			this.firstCategoryId = params.nav1_id || ''
			this.getList1()
			this.getList2()
		},
		onPullDownRefresh() {
			this.getList1()
		},
		onReachBottom() {
			this.getList1_1()
		},
		methods: {
			// 获取商品全部类目
			getList2() {
				this.$http.post(this.$api.shop_tabs_1).then(result => {
					if (result.code === 200) {
						this.nav1 = [{name: '全部'}].concat(result.data);
					} else {
						this.nav1 = []
					}
				}).catch(() => {
					this.nav1 = []
				})
			},
			// 根据条件查询店铺
			getList1() {
				this.page = 1
				this.loading = true
				this.status = 'more'
				this.$http.post(this.$api.shop_list_9, {}, {
					params: {
						pageNum: this.page,
						firstCategoryId: this.firstCategoryId,
						categoryId: '', // 商品二级分类id
						keyword: this.keyword, // 关键字
						status: this.type, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						area: this.filters.address.join('-'), // 区域 河南省-郑州市-中原区(中间用-隔开)
					}
				}).then(result => {
					uni.stopPullDownRefresh()
					this.loading = false
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list) && list.length > 0) {
							this.list = list
							if (this.list.length < 10) {
								this.status = 'noMore'
							}
						} else {
							this.list = []
							this.status = 'more'
						}
					} else {
						this.list = []
						this.status = 'more'
					}
				}).catch(() => {
					this.loading = false
					this.list = []
					this.status = 'more'
				})
			},
			// 根据条件查询店铺（缺）
			getList1_1() {
				if (this.status === 'noMore') return
				this.page++
				this.status = 'loading'
				this.$http.post(this.$api.shop_list_9, {}, {
					params: {
						firstCategoryId: this.firstCategoryId,
						categoryId: '', // 商品二级分类id
						pageNum: this.page,
						keyword: this.keyword, // 关键字
						status: this.type, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						area: this.filters.address.join('-'), // 区域 河南省-郑州市-中原区(中间用-隔开)
					}
				}).then(result => {
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list)) {
							if (list.length > 0) {
								this.list = this.list.concat(list);
							} else {
								this.status = 'noMore'
							}
						} else {
							this.status = 'more'
						}
					}
				}).catch(() => {
					this.status = 'more'
				})
			},
			resetFilter() {
				this.currentIndex1 = null
				this.address = []
				this.firstCategoryId = ''
				this.filters = {
					firstCategoryId: '',
					address: [],
				}
				this.comfirmFilter()
			},
			// 确认筛选
			comfirmFilter() {
				this.filters = {
					firstCategoryId: this.firstCategoryId,
					address: this.address,
				}
				this.show1 = false
				this.getList1()
			},
			// 获取商品全部类目
			getList2() {
				this.$http.post(this.$api.shop_tabs_1).then(result => {
					if (result.code === 200) {
						this.nav1 = result.data;
					}
				})
			},
			changeTab1(item,index) {
				this.currentIndex1 = index
				this.firstCategoryId = item.id 
			},
			// 切换排序类型
			changeTab3(index) {
				this.currentIndex3 = index
				this.type = String(index)
				// if(index === 2) this.sortx = this.sortx === 1 ? 0 : 1
				this.getList1()
			},
			// 切换排版
			changeTab4(row_type) {
				this.row_type = row_type === 'row' ? 'table' : 'row'
			},
			// 打开筛选条件弹窗
			changeTab5() {
				this.show1 = true
			},
			// 取消时的停靠的数据,下次打开在这个位置
			onCancel(e) {
				console.log(e)
			},
			// 三级联动选择
			showMulLinkageThreePicker() {
				this.$refs.mpvueCityPicker.show()
			},
			// 处理联动数据
			onConfirm(e) {
				this.pickerText = JSON.stringify(e)
				this.pickerViewText = JSON.parse(this.pickerText)
				this.address = this.pickerViewText.label.split('-')
			},
			// 去店铺详情
			goRouter1(item) {
				if (Object.prototype.toString.call(item) === '[object Object]' && typeof item.id === 'string' && item.id !== '') {
					uni.navigateTo({
						url: `../shop/index1?sid=${item.id}`
					});
				} else {
					console.log('店铺id错误');
				}
			},
			// 去商品详情
			goRouter10(item) { 
				if (Object.prototype.toString.call(item) === '[object Object]' && typeof item.id === 'string' && item.id !== '') {
					uni.navigateTo({
						url: `/pagesShop/details/details?cid=${item.id}`
					});
				} else {
					console.error("商品id错误",item.id);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.filter-selects {
		display: flex;
		flex-wrap: wrap;
	}
	
	.filter-select {
		background-color: #ebebeb;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #999;
		width: 200rpx;
		height: 60rpx;
		padding: 0 20rpx;
		margin: 0 20rpx 20rpx 0;
		
		image {
			width: 20rpx;
			height: auto;
		}
	}
	
	.filter-inputs {
		display: flex;
		align-items: center;
		
		input {
			background-color: #ebebeb;
			height: 80rpx;
			border-radius: 40rpx;
			text-align: center;
			font-size: 30rpx;
		}
		
		text {
			margin: 0 10rpx;
			color: #ccc;
		}
	}
	
	.filter-tags {
		display: flex;
		flex-wrap: wrap;
	}
	
	.filter-tag {
		border: 1px solid #ddd;
		font-size: 26rpx;
		padding: 5rpx 10rpx;
		flex-shrink: 0;
		margin: 0 20rpx 20rpx 0;
		border-radius: 4rpx;
		
		&.active {
			background-color: $uni-color-error;
			color: #fff;
			border: 0;
		}
	}
	
	.filter-wapper {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1;
		width: 100%;
		height: 100%;
		background-color: rgba(0,0,0,0.5);
		display: flex;
		justify-content: flex-end;
	}
	
	.filter-extra {
		flex-grow: 1;
		height: 100%;
	}
	
	.filter-content {
		width: 80%;
		height: 100%;
		background-color: #fff;
		box-sizing: border-box;
		padding: calc(var(--status-bar-height) + 54px) 30rpx 0;
		display: flex;
		flex-direction: column;
	}
	
	.filter-h4 {
		font-size: 30rpx;
		margin: 30rpx 0;
	}
	
	.filter-box {
		flex-grow: 1;
	}
	
	.filter-btns {
		height: 60px;
		display: flex;
		justify-content: center;
		align-items: center;
		
		button {
			width: 200rpx;
			height: 80rpx;
			background-color: #ebebeb;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0;
			border-radius: 0;
			
			&:nth-of-type(1) {
				border-radius: 40rpx 0 0 40rpx;
			}
			&:nth-of-type(2) {
				border-radius: 0 40rpx 40rpx 0;
				background-color: $uni-color-error;
				color: #fff;
			}
		}
	}
	
	.nav-center {
		text-align: left !important;
		padding: 0 !important;
		position: relative;
		
		input {
			width: 450rpx;
			background-color: #fff;
			color: #333;
			height: 30px;
			border-radius: 30px;
			box-sizing: border-box;
			padding: 0 20rpx 0 70rpx;
			font-size: 26rpx;
		}
		
		image {
			width: 16px;
			height: 16px;
			position: absolute;
			left: 12px;
			top: 8px;
		}
	}
	
	.card-info-right {
		display: flex;
		
		&>view {
			margin-left: 10rpx;
		}
	}
	
	.card {
		margin: 30rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
	}
	
	.card-left {
		flex-shrink: 0;
		width: 180rpx;
		height: 180rpx;
		overflow: hidden;
		margin-right: 20rpx;
		
		image {
			width: 100%;
			height: auto;
		}
	}
	
	.card-right {
		flex-grow: 1;
	}
	
	.card-title {
		font-size: 28rpx;
	}
	
	.card-info {
		margin-top: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.card-price {
		text {
			&:nth-of-type(1) {
				color: $uni-color-error;
				font-size: 36rpx;
				font-weight: bold;
			}
			&:nth-of-type(2) {
				margin-left: 10rpx;
				color: #999;
				text-decoration: line-through;
			}
		}
	}
	
	.card-xl {
		font-size: 24rpx;
		color: #999;
	}
	
	.card-tools {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.card-time {
		margin-top: 20rpx;
		color: $uni-color-error;
		font-size: 24rpx;
	}
	
	.card-wapper {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin: 30rpx;
		
		.card {
			margin: 0 0 30rpx;
			padding: 0;
			flex-direction: column;
			width: 330rpx;
		}
		
		.card-tools {
			flex-direction: column;
			align-items: flex-start;
			margin-bottom: 10rpx;
		}
		
		.card-info {
			margin-top: 10rpx;
		}
		
		.card-time {
			margin-top: 10rpx;
		}
	}
	
	.card-top {
		width: 330rpx;
		height: 330rpx;
	}
	
	.card-bottom {
		padding: 10rpx;
	}
	
	.fixed-tool-top {
		position: fixed;
		top: calc(var(--status-bar-height) + 44px);
		left: 0;
		z-index: 1;
		width: 100%;
		height: 40px;
		box-sizing: border-box;
		background-color: #fff;
	}
	
	.fl1,.fl2 {
		border-bottom: 1px solid #eee;
		box-sizing: border-box;
	}
	
	.fltab {
		height: 100%;
		display: flex;
	}
	
	.fltab-item {
		display: flex;
		align-items: center;
		flex-shrink: 0;
		height: 100%;
		font-size: 26rpx;
		padding: 0 20rpx;
		position: relative;
		
		&.active {
			color: $blue;
			
			&::after {
				display: block;
			}
		}
	}
	
	.fltab-item1 {
		&::after {
			content: "";
			display: none;
			position: absolute;
			bottom: 0;
			left: 50%;
			margin-left: -0.5em;
			width: 1em;
			height: 6rpx;
			background-color: $blue;
		}
		&.active {
			&::after {
				display: block;
			}
		}
	}
	
	.sx-box {
		height: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.sx-box-item {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-grow: 1;
		font-size: 28rpx;
		& > image {
			width: 36rpx;
			height: auto;
		}
		
		&.active {
			color: $blue;
		}
	}
	
	.sx-box-icon {
		width: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;
		
		image {
			width: 20rpx;
			height: auto;
			
			&:nth-of-type(1) {
				margin-bottom: 4rpx;
			}
		}
		
		.sbi1 {
			width: 25rpx;
		}
	}
	
	.content {
		padding: calc(var(--status-bar-height) + 124px) 0 0;
	}
	
	.tjshop {
		background-color: #fff;
		padding: 0 30rpx;
		margin: 30rpx 0;
	}
	
	.tjshop-content {}
	
	.tj {
		background-color: #fff;
		padding: 30rpx;
	}
	
	.tc {
		padding: 30rpx 0;
	}
	
	.tc-top {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.tc-top-left {
		display: flex;
		align-items: center;
	
		
	}

	
	.ttl-info-title {
		font-size: 28rpx;
	}
	
	.ttl-info-content {
		margin-top: 6rpx;
		display: flex;
		font-size: 24rpx;
		color: #999;
	
		view {
			margin-right: 40rpx;
	
			text {
				color: $uni-color-warning;
			}
		}
	}
	
	.tc-top-right {
		button {
			&::after {
				display: none;
			}
	
			border: 1px solid $uni-color-warning;
			border-radius: 25rpx;
			padding: 0;
			font-size: 26rpx;
			color: $uni-color-warning;
			width: 5em;
			height: 2em;
			line-height: 2em;
		}
	}
	
	.tc-bottom {
		padding-top: 20rpx;
		display: flex;
		// justify-content: space-between;
	}
	
	.tc-bottom-img {
		width: 223rpx;
		position: relative;
	
		&>image {
			width: 223rpx;
			height: 223rpx;
		}
	}
	
	.tcbp {
		position: absolute;
		bottom: 6rpx;
		left: 0;
		padding: 5rpx 10rpx;
		background-color: rgba(0, 0, 0, 0.5);
		color: $uni-color-error;
		border-radius: 0 8rpx 0 8rpx;
	}
	
</style>
